//
//  WBWelcomeView.swift
//  simpleWeibo
//
//  Created by sunmumu on 2018/9/24.
//  Copyright © 2018年 孙林. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLable: UILabel!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    
    class func welcomeView() -> WBWelcomeView {
        let nib = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        //从XIB加载的视图 默认是600x600大小
        v.frame = UIScreen.main.bounds
        return v
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        //提示: initWithCode 只是刚刚从XIB的二进制文件将视图数据加载完成
        //还没有和代码连线建立起关系, 所以开发时 千万不要在这个方法中处理UI
    }
    
    override func awakeFromNib() {
        //1 url
        guard let urlString = WBNetWorkManager.shared.userAccount.avatar_large,
            let url = URL(string: urlString)
            else {
                return
        }
        
        //2 设置头像 - 如果网络没有下载完成, 先显示占位图片
        // 如果不指定占位图像, 之前设置的图像会被清空!
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"), options: [], completed: nil)
        //3 设置头像圆角
        iconView.layer.cornerRadius = iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
        
    }
    
    ///视图被添加到window上 表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        bottomConstraint.constant = bounds.size.height - 200
        UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            // 更新约束
            self.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 1.0, animations: {
                self.tipLable.alpha = 1
            }, completion: { (_) in
                self.removeFromSuperview()
            })
        }
    }
    
}
